package com.mobilepay.moneypig.model.activity.mine;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mobilepay.moneypig.R;
import com.mobilepay.moneypig.base.BaseActivity;
import com.mobilepay.moneypig.config.NetConfig;
import com.mobilepay.moneypig.model.application.App;
import com.mobilepay.moneypig.okhttp.HttpUtils;
import com.mobilepay.moneypig.response.ResponseResult;
import com.mobilepay.moneypig.response.mine.UserDetailData;
import com.mobilepay.moneypig.utils.CommonUtil;
import com.mobilepay.moneypig.utils.GsonUtil;

/**
 * Created by xlm on 2017/7/24 0024
 */

public class UserInfoActivity extends BaseActivity {
    private static String TAG = "UserInfoActivity";
    private RelativeLayout mRelativeBack;
    private TextView mTextUsername, mTextNumber, mTextRealName, mTextRealNumber;
    private TextView mTextIdentify;
    private LinearLayout mLinearUserIdentify;
    private RelativeLayout mRelativeIdentify;

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.activity_me_detail);
    }

    @Override
    protected void initViews() {
        mRelativeBack = (RelativeLayout) findViewById(R.id.detail_back);
        mTextUsername = (TextView) findViewById(R.id.detail_name);
        mTextNumber = (TextView) findViewById(R.id.detail_num);
        mTextRealName = (TextView) findViewById(R.id.detail_real_name);
        mTextRealNumber = (TextView) findViewById(R.id.detail_rela_num);
        mTextIdentify = (TextView) findViewById(R.id.detail_real);
        mLinearUserIdentify = (LinearLayout) findViewById(R.id.user_identify);
        mRelativeIdentify = (RelativeLayout) findViewById(R.id.detail_identify);
    }

    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {
        initData();
    }

    //更新用户信息
    private void initData() {
        UserDetailData.DataBean dataBean = App.getApp().getUserDetailInfo().getData();
        mTextNumber.setText(String.valueOf(dataBean.getUid()));
        mTextUsername.setText(dataBean.getUsername());
        int authType = dataBean.getReal_auth_status();
        if (authType == 0) {
            mTextIdentify.setText("去认证");
            mRelativeIdentify.setClickable(true);
            mLinearUserIdentify.setVisibility(View.GONE);
            mRelativeIdentify.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(IdentifyActivity.class, true, false);
                }
            });
        } else if (authType == 1) {
            mTextIdentify.setText("审核中");
            mRelativeIdentify.setClickable(false);
            mLinearUserIdentify.setVisibility(View.GONE);
        } else if (authType == 2) {
            mTextIdentify.setText("已认证");
            mTextRealName.setText(dataBean.getReal_name());
            mTextRealNumber.setText(CommonUtil.StringOpt(dataBean.getCards_code(), 5, 14));
            mRelativeIdentify.setClickable(false);
            mLinearUserIdentify.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void onSetListener() {
        mRelativeBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.detail_back:
                finish();
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        getUserDetail();
    }

    //账户信息
    private void getUserDetail() {
        HttpUtils.RequestParam param = new HttpUtils.RequestParam();
        param.add("method", NetConfig.get_detail);
        param.add("uid", String.valueOf(App.getApp().getUserInfo().getData().getUid()));
        param.add("sign", param.getMd5String());
        HttpUtils.getInstance().params(param).executePost(new HttpUtils.HttpListener() {
            @Override
            public void onException(Throwable e) {
                super.onException(e);
                if (CommonUtil.isForeground(getThis())) {
                    showToastShort(getString(R.string.net_state));
                }
            }

            @Override
            public void onHttpSuccess(String string) {
                super.onHttpSuccess(string);
                if (HttpUtils.isJson(string)) {
                    ResponseResult responseResult = GsonUtil.GsonToBean(string, ResponseResult.class);
                    if (responseResult.getStatus() == 1) {
                        UserDetailData data = GsonUtil.GsonToBean(string, UserDetailData.class);
                        App.getApp().setUserDetailInfo(data);//保存账户信息
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                initData();
                            }
                        });
                    } else {
                        showToastShort(responseResult.getInfo());
                    }
                }
            }
        });
    }
}
